package com.lfs.market.task;

import com.lfs.market.model.entity.Orders;
import com.lfs.market.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.lfs.market.constants.OrderConstant.PAST_DUE;

/**
 * 定时任务用于标记过期订单
 */
@Slf4j
@Component
@EnableScheduling
public class OrderTask {
    @Autowired
    OrdersService ordersService;

    @Scheduled(fixedRate = 60000) // 每10分钟执行一次
    public void checkAndMarkExpiredOrders() {
        List<Orders> expiredOrders = ordersService.orderTaskDelete();
        for (Orders order : expiredOrders) {
            order.setStatus(PAST_DUE); // 标记为订单过期
            ordersService.updateById(order);
            log.info("订单{}已标记为过期", order.getOrderNo());
        }
    }
}
